package org.example.recruitserver.Controller;

import jakarta.annotation.Resource;
import org.example.recruitserver.mapper.RecruitListMapper;
import org.example.recruitserver.model.*;
import org.example.recruitserver.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class AJobListController {

    @Autowired
    RecruitListMapper recruitListMapper;

    /**
     * 根据Id获取岗位信息
     *
     * @return
     */
    @GetMapping("/getAJobList")
    public AJobListModel getAJobList() {
        return recruitListMapper.findId(1);
    }

    @GetMapping("/getAllRecruitProvince")
    public AdderNameModel getAllAdderRecruit() {
        List<AdderNameModel> all = recruitListMapper.findAll();
        return all.get(0);
    }

    /**
     * 获取省份岗位总数量
     * @return
     */
    @GetMapping("/getAllProvinceRecruit")
    public AllCityDataModel<AdderNameModel> getAllDataProvince() {
        AllCityDataModel<AdderNameModel> adderNameModelAllCityDataModel = null;
        // 新数据
        ArrayList<AdderNameModel> newAdderList = new ArrayList<>();
        // 获取省份岗位数量
        List<AdderNameModel> allAdderName = recruitListMapper.findAll();
        // 获取前34个省份名称
        List<ProvinceModel> allProvince = recruitListMapper.findProvinceAll(34);

        for (AdderNameModel anm : allAdderName) {
            List<ProvinceModel> likeProvinceAll = recruitListMapper.findLikeProvinceAll(anm.getAdderName());
            // 遍历数据获取城市Id
            for (ProvinceModel cityId : likeProvinceAll) {
                // 遍历获取省份Id
                for (ProvinceModel provinceName: allProvince) {
                    // 判断城市id是否对应省份Id
                    if (provinceName.getArea_id().equals(cityId.getParent_id())) {
                        // 将新数据存储到集合中
                        if (provinceName.getName() != null) {
                            newAdderList.add(new AdderNameModel(anm.getNumberSize(), provinceName.getName()));
                        }
                    }
                }
            }
        }

        // 数据出错，台湾省没有添加进去
        newAdderList.add(new AdderNameModel("30","台湾省"));

        List<AdderNameModel> list = new Utils().getNewAdderList(newAdderList);
        if (list != null) {
            adderNameModelAllCityDataModel = new AllCityDataModel<>();
            adderNameModelAllCityDataModel.setCode("200");
            adderNameModelAllCityDataModel.setMsg("获取成功");
            adderNameModelAllCityDataModel.setData(list);
        } else {
            adderNameModelAllCityDataModel = new AllCityDataModel<>();
            adderNameModelAllCityDataModel.setCode("500");
            adderNameModelAllCityDataModel.setMsg("服务器出错!");
            adderNameModelAllCityDataModel.setData(list);
        }
        return adderNameModelAllCityDataModel;
    }


    /**
     *
     * @param psm
     * @return
     */
    @GetMapping("/recruit/allLanguageType")
    public AllCityDataModel<AJobListModel> getAllLanguageType(@RequestBody PageSizeModel psm) {
        // 获取页数
        int page = Integer.parseInt(psm.getPage());
        // 获取每页数据量
        int pageSize = Integer.parseInt(psm.getPageSize());
        // 计算偏移量
        int offset = (page - 1) * pageSize;
//        System.out.println(psm.getLanguageType());
//        System.out.println(psm.getPage());
//        System.out.println(psm.getPageSize());

        List<AJobListModel> typeAll = recruitListMapper.findTypeAll(psm.getLanguageType(), offset, pageSize);

        AllCityDataModel<AJobListModel> allCityDataModel = new AllCityDataModel<>();
        if (typeAll != null) {
            allCityDataModel.setCode("200");
            allCityDataModel.setMsg("获取成功!");
            allCityDataModel.setData(typeAll);
        } else {
            allCityDataModel.setCode("500");
            allCityDataModel.setMsg("获取数据失败!");
            allCityDataModel.setData(typeAll);
        }
        return allCityDataModel;
    }
}
